import java.util.Arrays;

/**
 * Created with IntelliJ IDEA
 * Description
 * User:华为
 * Date:2024/12/21
 * Time:10:37
 */
public class MyArrayList implements IList {
    public int[] array;
    public int UsedSize;
    public static final int DEFAULT_CAPACITY = 10;

    public MyArrayList(){
        this.array = new int[DEFAULT_CAPACITY];
     }

    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }

        this.array[this.UsedSize] = data;
        this.UsedSize++;

    }
    public boolean isFull(){
        return this.UsedSize == this.array.length;
    }
    private void grow(){
        this.array = Arrays.copyOf(this.array,2*this.array.length);
    }
    private void checkPos(int pos) throws PosIllegal{
        if(pos < 0 || pos > UsedSize ){
            throw new PosIllegal("Pos位置不合法");
        }
    }
    @Override

    public void add(int pos, int data) {
        try {
            checkPos(pos);

            if(isFull()){
                grow();
            }
            for (int i = UsedSize-1; i >= pos; i--) {
                array[i+1]= array[i];
            }
            array[pos] = data;
            this.UsedSize++;

        }catch (PosIllegal e){
            System.out.println("插入元素pos位置不合法");
            e.printStackTrace();
        }


    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < UsedSize; i++) {
            if(array[i] == toFind){
                return true;
            }
        }
        return false;
    }

    /**
     * 包含 toFind返回下标
     * 不包含返回 -1
     * @param toFind
     * @return
     */
    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < UsedSize; i++) {
            if (array[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    private void CheckPos2(int pos){
        if(pos < 0 || pos >= UsedSize){
            throw new PosIllegal("Pos位置不合法");

        }
    }
    private void checkEmpty ()throws EmptyException{
        if(isEmpty()){
            throw new EmptyException("顺序表为空");
        }
    }
    public Boolean isEmpty(){
        return UsedSize == 0;
    }

    @Override
    public int get(int pos) {
        try {
            checkEmpty();
            CheckPos2(pos);
            return array[pos];

        }catch (EmptyException e){
            e.printStackTrace();
        }catch (PosIllegal e){
            e.printStackTrace();
        }
        return -1;

    }

    @Override
    public void set(int pos, int value) {
        try {
            checkEmpty();
            CheckPos2(pos);
            array[pos] = value;

        }catch (EmptyException e){
            e.printStackTrace();
        }catch (PosIllegal e){
            e.printStackTrace();
        }

    }

    @Override
    public void remove(int toRemove) {
        try {
            checkEmpty();
            int pos = indexOf(toRemove);
            if(pos == -1){
                System.out.println("没有要删除的数据");
                return;
            }
            for (int i = pos; i <UsedSize-1 ; i++) {
                array[i]= array[i+1];
            }
            UsedSize--;

        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.UsedSize;
    }

    @Override
    public void clear() {
//        for (int i = 0; i < UsedSize; i++) {
//            array[i] = 0;
//        }
        UsedSize = 0;

    }

    @Override
    public void display() {
        for (int i = 0; i < UsedSize; i++) {
            System.out.print(array[i]+" ");
        }

    }
}
